package com.burst.reptile.modular.zp.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.NumberUtils;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.zp.entity.ZpComment;
import com.burst.reptile.modular.zp.mapper.ZpCommentMapper;
import com.burst.reptile.modular.zp.service.IZpCommentService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 评论列表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-30
 */
@Service
public class ZpCommentServiceImpl extends ServiceImpl<ZpCommentMapper, ZpComment> implements IZpCommentService {

	@Override
	public String add(ZpComment comment) {
		comment.setCtype("0");
		comment.setContent(StrUtil.trim(comment.getContent()));
		comment.setUserId(SecurityUtils.getLoginUserId());
		this.baseMapper.insert(comment);
		return comment.getCid() + "";
	}

	@Override
	public String reply(ZpComment comment) {
		Long pcid = comment.getPcid();
		Long touserId = comment.getTouserId();
		Long acid = comment.getAcid();
		if(ToolUtil.isEmpty(pcid)) {
			throw new CustomException("非法评论");
		}
		if(ToolUtil.isEmpty(touserId)) {
			throw new CustomException("非法评论");
		}
		if(ToolUtil.isEmpty(acid)) {
			throw new CustomException("非法评论");
		}
		comment.setCtype("1");
		comment.setUserId(SecurityUtils.getLoginUserId());
		this.baseMapper.insert(comment);
		return comment.getCid() + "";
	}

	@Override
	public Page<ZpComment> pageList(Page<ZpComment> page, String id, String order) {
		QueryWrapper<ZpComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ctype", 0).eq("article_id", id);
		if(StrUtil.equals(order, "1")) {
			queryWrapper.orderByDesc("create_time");
		}else {
			queryWrapper.orderByDesc("likes", "create_time");
		}
		return this.page(page, queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int del(String id) {
		ZpComment byId = this.getById(id);
		if(ToolUtil.isNotEmpty(byId)) {
			Long userId = byId.getUserId();
			Long loginUserId = SecurityUtils.getLoginUserId();
			if(!loginUserId.equals(userId)) {
				throw new CustomException("非法删除");
			}
			String ctype = byId.getCtype();
			if(StrUtil.equals(ctype, "0")) { // 评论
				// 删除评论下面的所有回复
				this.baseMapper.delete(new QueryWrapper<ZpComment>().eq("acid", id));
			}
		}
		return this.baseMapper.deleteById(id);
	}

	@Override
	public String getCommentNum(String id) {
		int count = this.count(new QueryWrapper<ZpComment>().eq("article_id", id));
		return NumberUtils.amountConversion(count);
	}

	@Override
	public Page<ZpComment> pageReplyList(Page<ZpComment> page, String id) {
		QueryWrapper<ZpComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ctype", 1).eq("acid", id);
		queryWrapper.orderByDesc("likes", "create_time");
		return this.page(page, queryWrapper);
	}

	@Override
	public String wxAdd(ZpComment comment) {
		comment.setContent(StrUtil.trim(comment.getContent()));
		this.baseMapper.insert(comment);
		return comment.getCid() + "";
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int wxdel(String id, Long userId) {
		ZpComment byId = this.getById(id);
		if(ToolUtil.isNotEmpty(byId)) {
			Long cuserId = byId.getUserId();
			if(!userId.equals(cuserId)) {
				throw new CustomException("非法删除");
			}
			String ctype = byId.getCtype();
			if(StrUtil.equals(ctype, "0")) { // 评论
				// 删除评论下面的所有回复
				this.baseMapper.delete(new QueryWrapper<ZpComment>().eq("acid", id));
			}
		}
		return this.baseMapper.deleteById(id);
	}

	@Override
	public Page<ZpComment> pageList(Page<ZpComment> page, ZpComment comment) {
		QueryWrapper<ZpComment> queryWrapper = new QueryWrapper<>();
		Long userId = comment.getUserId();
		if(ToolUtil.isNotEmpty(userId)) {
			queryWrapper.eq("user_id", userId);
		}
		queryWrapper.eq("ctype", 0);
		queryWrapper.orderByDesc("create_time");
		return this.page(page, queryWrapper);
	}

}
